package com.comiee.tankwar;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
/**
 * 游戏窗口
 * @author comiee
 *
 */
public class GameScreen implements Screen {
	private SpriteBatch batch;
	private Stage stage;	//舞台
	private OrthographicCamera camera;	//相机
	private Touchpad touchpad;	//摇杆
	private Button button;	//开火键
	private Label label;	//分数显示
	List<Sprite> all;	//所有的碰撞箱
	Terrain terrain;	//地形
	Tank tank;	//玩家
	List<Tank> enemy;	//敌人
	private int level=1; 	//等级
	private int mapIndex=0;	//地图编号
	private TiledMap map;	//地图
	private OrthogonalTiledMapRenderer renderer;	//地图渲染器
	
	@Override
	public void show() {
		batch = new SpriteBatch();
		stage = new Stage();
		Gdx.input.setInputProcessor(stage);
		camera = new OrthographicCamera();
		camera.setToOrtho(false);
		//摇杆
		Touchpad.TouchpadStyle touchpadStyle = new Touchpad.TouchpadStyle();
		touchpadStyle.background = new TextureRegionDrawable(new TextureRegion(new Texture("images/tochpad_background.png")));
		touchpadStyle.knob = new TextureRegionDrawable(new TextureRegion(new Texture("images/tochpad_knob.png")));
		touchpad = new Touchpad(15,touchpadStyle);
		touchpad.setBounds(0, 0, 500, 500);
		stage.addActor(touchpad);
		//开火键
		Button.ButtonStyle buttonStyle = new Button.ButtonStyle();
		buttonStyle.up = new TextureRegionDrawable(new TextureRegion(new Texture("images/tochpad_knob.png")));
		buttonStyle.down = new TextureRegionDrawable(new TextureRegion(new Texture("images/tochpad_background.png")));
		button = new Button(buttonStyle);
		button.setPosition(Gdx.graphics.getWidth()-250-button.getWidth()/2, 250-button.getHeight()/2);
		stage.addActor(button);
		//分数显示
		Label.LabelStyle labelStyle = new Label.LabelStyle(new BitmapFont(), Color.BLUE);
		label = new Label("", labelStyle);
		label.setFontScale(4);
		label.setAlignment(Align.topLeft);	//重心位置
		label.setPosition(camera.position.x-(camera.viewportWidth/2), camera.position.y+(camera.viewportHeight/2));
		stage.addActor(label);
		//游戏元素
		all= new ArrayList<Sprite>();
		terrain = new Terrain(mapIndex);
		terrain.update();
		all.addAll(terrain.list1);	//只有list1和list2有碰撞方块
		all.addAll(terrain.list2);
		tank = new Tank(camera);
		all.add(tank);
		enemy = new ArrayList<Tank>();
		enemy.add(new Tank(all,level));
		all.addAll(enemy);
		//地图
		map = new TmxMapLoader().load("map/map1.tmx");
		renderer = new OrthogonalTiledMapRenderer(map);
		
	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		//玩家操作
		if (touchpad.isTouched()) {
			tank.move(touchpad.getKnobPercentX(), touchpad.getKnobPercentY(), all);
		}
		if (button.isPressed()) {
			tank.fire();
		}
		for (Missile i : tank.missiles) {
			i.live = i.test(enemy,terrain,all);
		}
		if (!tank.live) {	//玩家死亡
			all.remove(tank);
			tank=new Tank(camera);
			all.add(tank);
		}
		//电脑操作
		if (enemy.isEmpty()) {	//没有敌人生成新敌人
			if (level<=10) level++;	//等级提升，最大为10
			for(int i=0;i<level;i++) {
				Tank e = new Tank(all,level);
				enemy.add(e);
				all.add(e);
			}
		}
		for (Tank e : enemy) {
			List<Tank> tanks =new ArrayList<Tank>(); //该坦克可以伤害的坦克列表
			tanks.add(tank);
			tanks.addAll(enemy);
			tanks.remove(e);
			e.AI(tanks,all);
			for(Missile i : e.missiles) {
				i.live = i.test(tanks,terrain,all);
			}
		}
		//显示
		label.setText("score:"+tank.score);
		camera.update();
		//renderer.setView(camera);
		//renderer.render(new int[]{0,1,2});
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		terrain.draw(batch,1,2);
		tank.draw(batch);
		List<Tank> temp = new ArrayList<Tank>();	//存储死亡状态的坦克
		for(Tank i : enemy) {
			if (i.live==true) i.draw(batch);
			else temp.add(i);
		}
		enemy.removeAll(temp);
		all.removeAll(temp);
		terrain.draw(batch,3);
		batch.end();
		//renderer.render(new int[]{3});
		stage.act();
		stage.draw();
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO 自动生成的方法存根
	
	}

	@Override
	public void resume() {
		// TODO 自动生成的方法存根
	
	}

	@Override
	public void pause() {
		// TODO 自动生成的方法存根

	}

	@Override
	public void hide() {
		this.batch.dispose();
		this.stage.dispose();
		this.terrain.dispose();
		tank.dispose();
		for (Tank i : enemy) {
			i.dispose();
		}
	}

	@Override
	public void dispose() {
		// TODO 自动生成的方法存根
	
	}

}
